function []=load_exp(ExpID,adata_dir,dvh,load_ftypes)

load_2P=sum(strcmp(load_ftypes,'.bin'));
load_eye=sum(strcmp(load_ftypes,'.eye'));
load_run=sum(strcmp(load_ftypes,'.run'));
load_aux=sum(strcmp(load_ftypes,'.lvd'));
load_vid2=sum(strcmp(load_ftypes,'vid2'));

try
    load_noregister = evalin('base','load_noregister');
catch
    load_noregister = 0;
end

ud=get(dvh.hf,'UserData');
rdata_dir=ud.rdata_dir;
tmp_data_dir=ud.tmp_data_dir;

vars_to_assigin_base={};

% find the adata file
[adata_file,mouse_id,userID]=find_adata_file(ExpID,adata_dir);

if isempty(adata_file)
    disp('Found no Adata file - register data before loading');
    return;
end

try
    load([adata_dir '\' userID '\' mouse_id '\' adata_file]);
    if ~exist('mean_data','var')
        if ~exist('ROItrans','var');
            if isa(ROIs,'cell')
                ROItrans=cell(1,length(ROIs));
                for und=1:length(ROIs)
                    ROItrans{und}=zeros(3,1);
                end
            else
                ROItrans=zeros(3,1);
            end
        end
        vars_to_assigin_base={vars_to_assigin_base{:},'ROIs','ROItrans','np','bv','act_map'};
    else 
        load_2P=0;
        load_eye=0;
        load_run=0;
        load_aux=0;
        load_vid2=0;
        vars_to_assigin_base={vars_to_assigin_base{:},'mean_data'};
    end
catch
    disp('Could not load analyzed data!');
    return;
end


try
    nbr_piezo_layers=readini([rdata_dir userID '\' mouse_id '\' fnames{1}(1:end-3) 'ini'],'piezo.nbrlayers');
catch
    nbr_piezo_layers=1;
    disp('ATTENTION - could not read nbr of piezo layers from ini file')
end

try
    fpga_frames_zstep=readini([rdata_dir userID '\' mouse_id '\' fnames{1}(1:end-3) 'ini'],'FPGA.framesperzstep');
    if fpga_frames_zstep > 0
        nbr_piezo_layers=1;  % more likely setting was forgotten to be set to 1
    end
    fpga_frames_layer=readini([rdata_dir userID '\' mouse_id '\' fnames{1}(1:end-3) 'ini'],'FPGA.framesperlayer');
catch
    disp('ATTENTION - could not read z step details from ini file')
    fpga_frames_zstep=0;
    fpga_frames_layer=1;
end

if fpga_frames_zstep>0
    mean_data_fname=[adata_dir userID '\' mouse_id '\' adata_file];
    disp('This is a Z stack');
    load_2P = 0;
    load(mean_data_fname);
    vars_to_assigin_base = {vars_to_assigin_base{:},'mean_data'};
end

% load the 2P data
if load_2P
    % pre-allocate space for data
    if nbr_piezo_layers>1
        data=cell(1,nbr_piezo_layers);
        for knd=1:nbr_piezo_layers
            data{knd}=zeros(size(template{1},1),size(template{1},2),length(dx{knd}),'int16');
        end
    else
        data=zeros(size(template,1),size(template,2),length(dx),'int16');
    end
    
    % load the data
    for ind=1:length(fnames)
        if exist([tmp_data_dir userID '\' mouse_id '\' fnames{ind}],'file');
            disp(['loading file ' fnames{ind} ' from local tmpdata directory']);
            curr_load_path=[tmp_data_dir userID '\' mouse_id '\' fnames{ind}];
        else
            disp(['loading file ' fnames{ind} ' from the network RawData directory']);
            curr_load_path=[rdata_dir userID '\' mouse_id '\' fnames{ind}];
        end

        fi=fopen(curr_load_path,'r');
        x_res=fread(fi,1,'int16=>double');
        y_res=fread(fi,1,'int16=>double');
        
        if nbr_piezo_layers>1
            for knd=sum(nbr_frames(1:ind-1))+1:sum(nbr_frames(1:ind))
                data{rem(knd-1,nbr_piezo_layers)+1}(:,:,floor((knd-1)/nbr_piezo_layers)+1)=reshape(fread(fi,y_res*x_res,'int16=>int16'),y_res,x_res)';
            end
        else
            for knd=sum(nbr_frames(1:ind-1))+1:sum(nbr_frames(1:ind))
                data(:,:,knd)=reshape(fread(fi,y_res*x_res,'int16=>int16'),y_res,x_res)';
            end
        end
        fclose(fi);
    end
    
    if ~load_noregister
        disp(['Now registering data on dx dy values and correcting line shift']);
        if ~isa(dx,'cell')
            data=shift_data(data,dx,dy);
            data=correct_line_shift(data,mean(data,3));
        else
            for rnd=1:length(dx)
                data{rnd}=shift_data(data{rnd},dx{rnd},dy{rnd});
                data{rnd}=correct_line_shift(data{rnd},mean(data{rnd},3));
            end
        end
    else
        warning('loaded data without registration')
    end
    vars_to_assigin_base={vars_to_assigin_base{:}, 'data', 'act_map'};
end

if load_aux
    % load the aux data - first try loading the data from the local tmpdata
    % directory
    aux_data=[];
    for ind=1:length(aux_files)
        if exist([tmp_data_dir userID '\' mouse_id '\' aux_files{ind}],'file');
            disp(['loading file ' aux_files{ind} ' from local tmpdata directory']);
            curr_load_path=[tmp_data_dir userID '\' mouse_id '\' aux_files{ind}];
        else
            disp(['loading file ' aux_files{ind} ' from the network RawData directory']);
            curr_load_path=[rdata_dir userID '\' mouse_id '\' aux_files{ind}];
        end
        
        tmp_data=load_lvd(curr_load_path);
        tmp_frames=get_frame_times(tmp_data(2,:));
        
        if nbr_frames(ind)~=length(tmp_frames)
            if max(diff(tmp_frames))/median(diff(tmp_frames))<1.5 & length(tmp_frames)>nbr_frames(ind)
                disp('NC WARNING! Probably scanning too fast for FPGA to stop in the same frame')
                disp(['--- ' num2str(length(tmp_frames)-nbr_frames(ind)) ' frames too many ---'])
                tmp_data(2,tmp_frames(nbr_frames(ind)+1):end)=0;
            else
                disp('CRITICAL WARNING! - aux data longer than it should be.')
                %return
                %tmp_data=tmp_data(:,1:tmp_frames(nbr_frames(ind)+1));
                [~,last_bad_frame]=max(diff(tmp_frames));
                tmp_data=tmp_data(:,ceil(mean([tmp_frames(last_bad_frame) tmp_frames(last_bad_frame+1)])):end);
            end
        end
        aux_data(:,end+1:end+length(tmp_data))=tmp_data;
    end
    frame_times=get_frame_times(aux_data(2,:));
    vars_to_assigin_base={vars_to_assigin_base{:}, 'aux_data', 'frame_times'};
end

if load_run
    rdata=zeros(0,0,0,'int16');
    rmeta_data=[];
    no_run_data=0;
    for ind=1:length(aux_files)
        if exist([tmp_data_dir userID '\' mouse_id  '\' aux_files{ind}(1:end-4) '.run'],'file')
            disp(['loading file ' aux_files{ind}(1:end-4) '.run' ' from local tmpdata directory']);
            curr_load_path=[tmp_data_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.run'];
        elseif exist([rdata_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.run'],'file')
            disp(['loading file ' aux_files{ind}(1:end-4) '.run' ' from the network RawData directory']);
            curr_load_path=[rdata_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.run'];
        else
            disp('No run data found!')
            no_run_data=1;
            break
        end
        [tmp_rdata, tmp_rmeta_data] = load_vid_data(curr_load_path,'int16');
        nbr_rframes(ind)=length(tmp_rmeta_data);
        if ind==1||sum(size(rdata(:,:,1))==size(tmp_rdata(:,:,1)))==2
            rdata(:,:,end+1:end+size(tmp_rdata,3))=tmp_rdata;
        else
            disp('Idata sizes don''t match - will improvise');
            rdata(1:size(tmp_rdata,1),1:size(tmp_rdata,2),end+1:end+size(tmp_rdata,3))=tmp_rdata;
        end
        rmeta_data(:,end+1:end+length(tmp_rmeta_data))=tmp_rmeta_data;
    end

    if ~no_run_data
        rframe_times=get_iframe_times(rmeta_data,aux_data(1,:),nbr_rframes);
        vars_to_assigin_base={vars_to_assigin_base{:}, 'rdata', 'rmeta_data','rframe_times', 'nbr_rframes'};
    end
end

if load_vid2
    v2data=zeros(0,0,0,'int16');
    v2meta_data=[];
    no_vid2_data=0;
    for ind=1:length(aux_files)
        if exist([tmp_data_dir userID '\' mouse_id  '\' aux_files{ind}(1:end-4) '.vid2'],'file')
            disp(['loading file ' aux_files{ind}(1:end-4) '.vid2' ' from local tmpdata directory']);
            curr_load_path=[tmp_data_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.vid2'];
        elseif exist([rdata_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.vid2'],'file')
            disp(['loading file ' aux_files{ind}(1:end-4) '.vid2' ' from the network RawData directory']);
            curr_load_path=[rdata_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.vid2'];
        else
            disp('No run data found!')
            no_vid2_data=1;
            break
        end
        [tmp_v2data, tmp_v2meta_data] = load_vid_data(curr_load_path,'int16');
        nbr_v2frames(ind)=length(tmp_v2meta_data);
        if ind==1||sum(size(v2data(:,:,1))==size(tmp_v2data(:,:,1)))==2
            v2data(:,:,end+1:end+size(tmp_v2data,3))=tmp_v2data;
        else
            disp('vid2 data sizes don''t match - will improvise');
            v2data(1:size(tmp_v2data,1),1:size(tmp_v2data,2),end+1:end+size(tmp_v2data,3))=tmp_v2data;
        end
        v2meta_data(:,end+1:end+length(tmp_v2meta_data))=tmp_v2meta_data;
    end

    if ~no_vid2_data
        v2frame_times=get_iframe_times(v2meta_data,aux_data(1,:),nbr_v2frames);
        vars_to_assigin_base={vars_to_assigin_base{:}, 'v2data', 'v2meta_data','v2frame_times', 'nbr_v2frames'};
    end
end

if load_eye
    idata=zeros(0,0,0,'uint8');
    imeta_data=[];
    no_eye_data=0;
    for ind=1:length(aux_files)
        if exist([tmp_data_dir userID '\' mouse_id  '\' aux_files{ind}(1:end-4) '.eye'],'file');
            disp(['loading file ' aux_files{ind}(1:end-4) '.eye' ' from local tmpdata directory']);
            curr_load_path=[tmp_data_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.eye'];
        elseif exist([rdata_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.eye'],'file');
            disp(['loading file ' aux_files{ind}(1:end-4) '.eye' ' from the network RawData directory']);
            curr_load_path=[rdata_dir userID '\' mouse_id '\' aux_files{ind}(1:end-4) '.eye'];
        else
            disp('No eye data found!')
            no_eye_data=1;
            break
        end
        [tmp_idata, tmp_imeta_data] = load_eye_monitor_data(curr_load_path,load_eye);
        nbr_iframes(ind)=length(tmp_imeta_data);
        if ind==1||sum(size(idata(:,:,1))==size(tmp_idata(:,:,1)))==2
            idata(:,:,end+1:end+size(tmp_idata,3))=tmp_idata;
        else
            disp('Idata sizes don''t match - will improvise');
            idata(1:size(tmp_idata,1),1:size(tmp_idata,2),end+1:end+size(tmp_idata,3))=tmp_idata;
        end
        
        imeta_data(:,end+1:end+length(tmp_imeta_data))=tmp_imeta_data;
    end
    if ~no_eye_data
        [iframe_times]=get_iframe_times(imeta_data,aux_data(1,:),nbr_iframes);
        
        vars_to_assigin_base={vars_to_assigin_base{:}, 'idata', 'imeta_data','iframe_times', 'nbr_iframes'};
    end
end

vars_to_assigin_base={vars_to_assigin_base{:}, 'dx', 'dy', 'fnames', 'aux_files', 'nbr_frames', 'template', 'mouse_id','userID','adata_dir'};

% assign the variables in base
for ind=1:length(vars_to_assigin_base)
    try
        eval(['assignin(''base'',vars_to_assigin_base{ind},' vars_to_assigin_base{ind} ');']);
    end
end

disp('----Done loading Exp!----');
%EOF